<?php

namespace App\Modules\Admin\Http\Controllers;

use App\Models\Role;
use Illuminate\Support\Facades\Cache;
use App\Modules\Admin\Http\Requests\RoleCreateRequest;
use App\Modules\Admin\Http\Requests\RoleUpdateRequest;
use App\Modules\Admin\Http\Controllers\BaseController;
use App\Services\Admin\RoleService;
use Illuminate\Http\Request;


class RoleController extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $param = $this->getParam('Role','Role/getList');
        return view('admin::role.index',compact('param'));
    }

    /**
     * 获取列表页
     * @param Request $request
     * @method getList
     * @author chengzhigang<1256699215@qq.com>
     */
    public function getList(Request $request){
        Cache::forever('Role/getList', $request->server->get('QUERY_STRING'));
        $page = $request->input('page', 1);
        $per_page = $request->input('limit', 10);
        $roleService = new RoleService();
        $result = $roleService->getList($page,$per_page);
        if($result['status']==1){
            return layTableApi($result['data']['total'],$result['data']['data']);
        }else{
            return layTableApi(0,[],1);
        }
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        return view('admin::role.create');
    }

    public function show(){

    }

    /**
     * Store a newly created resource in storage.
     *
     *
     * @param RoleCreateRequest $request
     * @return \Illuminate\Http\Response
     */
    public function store(RoleCreateRequest $request)
    {
        //
        $data = $request->only(['name', 'display_name']);
        $data['guard_name'] = 'admin';
        Cache::forever('Role', true);
        if (Role::create($data)) {
            return redirect()->to(route('admin.role'))->with(['status' => '添加角色成功']);
        }
        return redirect()->to(route('admin.role'))->withErrors('系统错误！');
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $role = Role::findOrFail($id);
        return view('admin::role.edit', compact('role'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param RoleUpdateRequest $request
     * @param  int $id
     *
     * @return \Illuminate\Http\RedirectResponse
     */
    public function update(RoleUpdateRequest $request, $id)
    {
        //
        $role = Role::findOrFail($id);
        $data = $request->only(['name', 'display_name']);
        Cache::forever('Role', true);
        if ($role->update($data)) {
            return apiResponse('编辑成功',1,[]);
        }else{
            return apiResponse('编辑失败');
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function destroy(Request $request)
    {
        //
        $ids = $request->get('ids');
        if (empty($ids)) {
            return apiResponse('请选择删除项');
        }
        if (Role::destroy($ids)) {
            return apiResponse('删除成功！',1,[]);
        }
        return apiResponse('删除失败！');
    }

    public function permission($id)
    {
        $role = Role::findOrFail($id);
        $permissions = $this->permissionTree();
        foreach($permissions as $key1 => $item1) {
            $permissions[$key1]['own'] = $role->hasPermissionTo($item1['id']) ? 'checked': false;
            if (isset($item1['_child'])) {
                foreach($item1['_child'] as $key2 => $item2) {
                    $permissions[$key1]['_child'][$key2]['own'] = $role->hasPermissionTo($item2['id']) ? 'checked' :false;
                    if (isset($item2['_child'])) {
                        foreach($item2['_child'] as $key3 => $item3) {
                            $permissions[$key1]['_child'][$key2]['_child'][$key3]['own'] = $role->hasPermissionTo($item3['id']) ? 'checked' : false;
                        }
                    }
                }
            }
        }
        return view('admin::role.permission', compact('role', 'permissions'));
    }

    public function assignPermission(Request $request, $id)
    {
        $role = Role::findOrFail($id);
        $permissions = $request->get('permissions');

        if (empty($permissions)) {
            $role->permissions()->detach();
        } else {
            $role->syncPermissions($permissions);
        }
        return redirect()->to(route('admin.role'))->with(['status' => '已更新角色权限，请刷新整个浏览器']);
    }
}
